/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-10-01 10:09:11
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-10-15 23:49:26
 * @FilePath: \baking_machine\system\main.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "main.h"
#include "system_config.h"
#include "my_malloc.h"
#include "cJSON.h"
#include <stdio.h>
#include <string.h>
#include "axis.h"

int main(void)
{    
    SCB->VTOR = FLASH_BASE | 0x0000;
    __enable_irq();

    uint32_t ck_sys = rcu_clock_freq_get(CK_SYS);
    uint32_t ck_ahb = rcu_clock_freq_get(CK_AHB);
    uint32_t ck_apb1 = rcu_clock_freq_get(CK_APB1);
    uint32_t ck_apb2 = rcu_clock_freq_get(CK_APB2);
    SEGGER_RTT_printf(0, "sys=%d;ahb=%d;apb1=%d;apb2=%d.\r\n", \
                      ck_sys, ck_ahb, ck_apb1, ck_apb2);

    systick_config();
    system_board_init();

    while (1)
    {
        MOTOR_LED(0);
        delay_1ms(500);
        MOTOR_LED(1);
        delay_1ms(500);
        uint8_t test[4] = {0x55, 0xaa, 0x77, 0x88};
        bsp_uart3_write_bytes(test, 4);
        SEGGER_RTT_printf(0, "system running\r\n");
    }
}


void ADC0_1_IRQHandler(void)
{
    adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);

    extern axis_t axis;
    // run_state_machine_loop(&axis);
    
		axis.motor.iphase.iu = M0_W_CURRENT;
		axis.motor.iphase.iv = M0_U_CURRENT;
		axis.motor.iphase.iw = M0_V_CURRENT;
    static uint16_t times = 0;
    SEGGER_RTT_printf(0, "timer 16K = %d.\r\n", times++);
}

void TIMER2_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP)!=RESET)       
	{   
		timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
    }
}
